﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace TestTcpServer
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener list =
                new TcpListener(new IPEndPoint(IPAddress.Loopback, 5678));

            list.Start();

            TcpClient tcpcl = list.AcceptTcpClient();

            NetworkStream ns = tcpcl.GetStream();
            BinaryReader br = new BinaryReader(ns);
            BinaryWriter bw = new BinaryWriter(ns);

            // ns.ReadTimeout = 3 * 1000;

            tcpcl.ReceiveTimeout = 100;

            try
            {
                while (true)
                {
                    string msg = br.ReadString();

                    Console.WriteLine("Got message: {0}", msg);

                    // bw.Write(msg);
                }
            }
            catch (EndOfStreamException ex)
            {
                Console.WriteLine("Got exception: {0}", ex);
            }
            finally
            {
                br.Close();
                bw.Close();
                tcpcl.Close();
                list.Stop();
            }
        }
    }
}
